home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Libraries / TransEdit / FakeAlert.c < prev    next >
Text File  |  1994-02-21  |  7KB  |  230 lines

  1. /*
  2.  * In-memory item list for dialog with five items:
  3.  *
  4.  * 1    Button 1
  5.  * 2    Button 2
  6.  * 3    Button 3
  7.  * 4    caution icon
  8.  * 5    "^0^1^2^3" (static text)
  9.  * 6    user item (for outlining default button)
  10.  *
  11.  * The caller of FakeAlert passes the four strings that are to be
  12.  * substituted into the first item, the number of buttons that
  13.  * should be used, and the titles to put into each button.
  14.  * A copy of the item list is hacked to use the right number of
  15.  * buttons.
  16.  *
  17.  * Thanks to Erik Kilk and Jason Haines.  Some of the stuff to do
  18.  * this is modified from code they wrote.
  19.  *
  20.  * 17 Jun 92
  21.  * - Set the aDefItem field of the dialog template, so that return/enter
  22.  * key properly equivalence to clicking the default button.
  23.  * 05 Jun 93
  24.  * - Conversion for THINK C 6.0.
  25.  * 05 Jul 93
  26.  * - Repositioned buttons.  All are now in single line.  Button 1 is
  27.  * rightmost, button 2 in middle, and button 3 is leftmost.  As more
  28.  * buttons are used, they extend more to the left.
  29.  * 18 Dec 93
  30.  * - Took out all the "register" declarations.  The compiler's smart enough
  31.  * now that they don't make any difference, so they're just clutter.
  32.  * - Item list handle made unpurgable while ModalDialog() is processing
  33.  * dialog and purgable afterward.
  34.  * 18 Jan 94
  35.  * - Redid item list to provide a user item.  Default button outlining
  36.  * done using SkelSetDlogButtonOutliner().
  37.  * - Unused buttons are now hidden.  No more messing with changing the number
  38.  * of items in the item list.
  39.  * - Buttons positioned more in line with Apple guidelines.
  40.  * - Alert window positioned on the front window's parent device now.
  41.  * - FakeAlert() takes a parameter indicating the Cancel button.  This allows
  42.  * it to do key mapping for Escape and Command-period.
  43.  * 20 Jan 94
  44.  * - Added a caution icon to the alert item list.
  45.  */
  46.  
  47. # include    "TransSkel.h"
  48.  
  49. # include    "TransEdit.h"
  50.  
  51.  
  52. /* in-memory item list */
  53.  
  54.  
  55. typedef enum
  56. {
  57.     button1Item = 1,
  58.     button2Item,
  59.     button3Item,
  60.     cautionIconItem,
  61.     staticTextItem,
  62.     outlineItem
  63. };
  64.  
  65.  
  66. static short    itemList [] =
  67. {
  68.     5,                    /* max number of items - 1 */
  69.  
  70.     /*
  71.      * first button
  72.      */
  73.  
  74.     0, 0,                                /* reserve a long for item handle */
  75.     105, 260, 125, 340,                    /* display rectangle */
  76.     ((ctrlItem+btnCtrl) << 8) | 0,        /* pushButton, title 0 bytes long */
  77.  
  78.     /*
  79.      * second button
  80.      */
  81.  
  82.     0, 0,                                /* reserve a long for item handle */
  83.     105, 167, 125, 247,                    /* display rectangle */
  84.     ((ctrlItem+btnCtrl) << 8) | 0,        /* pushButton, title 0 bytes long */
  85.  
  86.     /*
  87.      * third button
  88.      */
  89.  
  90.     0, 0,                                /* reserve a long for item handle */
  91.     105, 20, 125, 100,                    /* display rectangle */
  92.     ((ctrlItem+btnCtrl) << 8) | 0,        /* pushButton, title 0 bytes long */
  93.  
  94.     /*
  95.      * icon item
  96.      */
  97.  
  98.     0, 0,                                /* reserve a long for item handle */
  99.     10, 10, 42, 42,                        /* display rectangle */
  100.     ((iconItem+itemDisable) << 8) | 2,    /* icon (disabled), icon id 2 bytes long */
  101.     2,                /* icon 2 */
  102.  
  103.     /*
  104.      * statText item
  105.      */
  106.  
  107.     0, 0,                                /* reserve a long for item handle */
  108.     10, 52, 58, 340,                    /* display rectangle */
  109.     ((statText+itemDisable) << 8) | 8,    /* statText (disabled), text 8 bytes long */
  110.     '^0', '^1',        /* ^0^1^2^3 */
  111.     '^2', '^3',
  112.  
  113.     /*
  114.      * user item for default button outlining
  115.      */
  116.  
  117.     0, 0,                                /* reserve a long for item handle */
  118.     0, 0, 10, 10,                            /* display rectangle (fake) */
  119.     ((userItem+itemDisable) << 8) | 0,    /* userItem (disabled), title 0 bytes long */
  120. };
  121.  
  122.  
  123. /*
  124.  * Hide a dialog button item by shifting it off into space.  HideDItem()
  125.  * does the same thing but unfortunately is not present on 64K ROM
  126.  * machines. Inside Macintosh IV-59 says you can hide an item by adding
  127.  * 16384 to the left and right coordinates of the bounding rectangle.
  128.  * However, that doesn't seem to work unless the control itself is moved
  129.  * as well.
  130.  */
  131.  
  132. static void
  133. HideDlogButton (DialogPtr dlog, short item)
  134. {
  135. Rect    r;
  136.  
  137.     SkelGetDlogRect (dlog, item, &r);
  138.     OffsetRect (&r, 16384, 0);
  139.     SkelSetDlogRect (dlog, item, &r);
  140.     MoveControl (SkelGetDlogCtl (dlog, item), 16384, 0);
  141. }
  142.  
  143.  
  144. /*
  145.  * Fake an alert, using an in-memory window and item list.
  146.  * The message to be presented is constructed from the first
  147.  * four arguments.  nButtons is the number of buttons to use,
  148.  * defButton is the default button, the next three args are
  149.  * the titles to put into the buttons.  The return value is
  150.  * the button number (1..nButtons).  This must be interpreted
  151.  * by the caller, since the buttons may be given arbitrary
  152.  * titles.
  153.  *
  154.  * nButtons should be between 1 and 3, inclusive.
  155.  * defButton should be between 1 and nButtons, inclusive.
  156.  * cancelButton should be between 1 and nButtons, inclusive, or
  157.  * 0 if there's no cancel button.
  158.  */
  159.  
  160. pascal short
  161. FakeAlert (StringPtr s1, StringPtr s2, StringPtr s3, StringPtr s4,
  162.             short nButtons, short defButton, short cancelButton,
  163.             StringPtr t1, StringPtr t2, StringPtr t3)
  164. {
  165. ModalFilterProcPtr    filter;
  166. GrafPtr        savePort;
  167. DialogPtr    dlog;
  168. Handle        iListHandle;
  169. Rect        bounds;
  170. short        item;
  171.  
  172.     InitCursor ();
  173.     GetPort (&savePort);
  174.     iListHandle = NewHandle (512L);
  175.     HLock (iListHandle);
  176.     BlockMove (&itemList, *iListHandle, 512L);
  177.     HUnlock (iListHandle);
  178.     HNoPurge (iListHandle);
  179.     SetRect (&bounds, 0, 0, 350, 135);
  180.     dlog = NewDialog (nil, &bounds, "\p", false, dBoxProc, (WindowPtr) -1L,
  181.                             false, 0L, iListHandle);
  182.     SkelPositionWindow (dlog, skelPositionOnParentDevice,
  183.                             FixRatio (1, 2), FixRatio (1, 5));
  184.     ((DialogPeek) dlog)->aDefItem = defButton;
  185.     SkelSetDlogButtonOutliner (dlog, outlineItem);
  186.  
  187.     SetPort (dlog);
  188.  
  189.     if (nButtons < 3)                /* hide unused buttons */
  190.     {
  191.         HideDlogButton (dlog, button3Item);
  192.         if (nButtons < 2)
  193.             HideDlogButton (dlog, button2Item);
  194.     }
  195.  
  196.     switch (nButtons)                /* set button titles */
  197.     {
  198.     case 3:
  199.         SetCTitle (SkelGetDlogCtl (dlog, button3Item), t3);
  200.         /* fall through... */
  201.     case 2:
  202.         SetCTitle (SkelGetDlogCtl (dlog, button2Item), t2);
  203.         /* fall through... */
  204.     case 1:
  205.         SetCTitle (SkelGetDlogCtl (dlog, button1Item), t1);
  206.     }
  207.  
  208.     ParamText (s1, s2, s3, s4);        /* construct message */
  209.     ShowWindow (dlog);
  210.  
  211.     /*
  212.      * Given a nil filter proc, ModalDialog() normally returns 1 if Return or
  213.      * Enter are hit.  This can be incorrect since the default item need not
  214.      * be one for the "alert" being presented here.  This problem does not
  215.      * actually occur since (i) a non-nil filter is installed by SkelDlogFilter()
  216.      * and (ii) the filter used by SkelDlogFilter() returns the item number in
  217.      * the aDefItem field.
  218.      */
  219.  
  220.     filter = SkelDlogFilter (nil, true);
  221.     if (cancelButton != 0)
  222.         SkelDlogCancelItem (cancelButton);
  223.     ModalDialog (filter, &item);
  224.     SkelRmveDlogFilter ();
  225.     HPurge (iListHandle);
  226.     DisposeDialog (dlog);    /* this also disposes of iListHandle */
  227.     SetPort (savePort);
  228.     return (item);
  229. }
  230.